using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The CourseTableDB class is responsible for interacting with the database to retrieve and store information
    /// about CourseTable objects.
    /// </summary>
    public class CourseTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of CourseTable from the underlying datasource.
        /// </summary>
        /// <param name="courseKey">The unique CourseKey of the CourseTable in the database.</param>
        /// <returns>An CourseTable when the CourseKey was found in the database, or null otherwise.</returns>
        public static CourseTable GetItem(int courseKey)
        {
            CourseTable myCourseTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocCourseTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@courseKey", courseKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myCourseTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myCourseTable;
            }
        }

        /// <summary>
        /// Returns a list with CourseTable objects.
        /// </summary>
        /// <returns>A generics List with the CourseTable objects.</returns>
        public static CourseTableList GetList()
        {
            CourseTableList tempList = new CourseTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocCourseTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Returns a list with CourseTable objects.
        /// <param name="schoolKey, standardLevel">Base table keys of the CourseTable in the database.</param>
        /// </summary>
        /// <returns>A generics List with the CourseTable objects.</returns>
        public static CourseTableList GetList(int SchoolKey, int StandardLevelKey)
        {
            CourseTableList tempList = new CourseTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocCourseTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@schoolKey", SchoolKey);
                    myCommand.Parameters.AddWithValue("@standardLevelKey", StandardLevelKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a CourseTable in the database.
        /// </summary>
        /// <param name="myCourseTable">The CourseTable instance to save.</param>
        /// <returns>The new CourseKey if the CourseTable is new in the database or the existing CourseKey when an item was updated.</returns>
        public static int Save(CourseTable myCourseTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocCourseTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myCourseTable.CourseKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@courseKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@courseKey", myCourseTable.CourseKey);
                    }

                    if (myCourseTable.SchoolKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", myCourseTable.SchoolKey);
                    }

                    if (myCourseTable.StandardLevelKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@standardLevelKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@standardLevelKey", myCourseTable.StandardLevelKey);
                    }

                    if (string.IsNullOrEmpty(myCourseTable.CourseCode))
                    {
                        myCommand.Parameters.AddWithValue("@courseCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@courseCode", myCourseTable.CourseCode);
                    }

                    if (string.IsNullOrEmpty(myCourseTable.CourseName))
                    {
                        myCommand.Parameters.AddWithValue("@courseName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@courseName", myCourseTable.CourseName);
                    }

                    if (string.IsNullOrEmpty(myCourseTable.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", myCourseTable.Description);
                    }
                    if (myCourseTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myCourseTable.Status);
                    }

                    //if (myCourseTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myCourseTable.CreatedDateTime);
                    //}
                    //if (myCourseTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myCourseTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a CourseTable from the database.
        /// </summary>
        /// <param name="courseKey">The CourseKey of the CourseTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int courseKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocCourseTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@courseKey", courseKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the CourseTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static CourseTable FillDataRecord(IDataRecord myDataRecord)
        {
            CourseTable myCourseTable = new CourseTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CourseKey")))
            {
                myCourseTable.CourseKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("CourseKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolKey")))
            {
                myCourseTable.SchoolKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StandardLevelKey")))
            {
                myCourseTable.StandardLevelKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StandardLevelKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CourseCode")))
            {
                myCourseTable.CourseCode = myDataRecord.GetString(myDataRecord.GetOrdinal("CourseCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CourseName")))
            {
                myCourseTable.CourseName = myDataRecord.GetString(myDataRecord.GetOrdinal("CourseName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
            {
                myCourseTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myCourseTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myCourseTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myCourseTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myCourseTable;
        }
    }

}
